//index.js
const app = getApp()
const apiurl = 'https://friend-guess.playonwechat.com/';
let sign = wx.getStorageSync('sign');
import tips from '../../utils/tips.js'
Page({
  data: {
    bgList1:[
      "255,255,255", "206,103,88", "211,120,76", "235,162,189", "243,239,166"
    ],
    bgList2:[
      "196,213,109", "116,191,184", "137,206,235", "110,101,180", "238,185,105"
    ],
    fontList1: [
      "255,255,255", "254,251,208", "246,202,203", "191,231,243", "245,193,156"
    ],
    fontList2:[
      "239,143,103", "118,112,112", "236,100,100", "211,226,195", "69,143,144"
    ],
    text:'禾葡兰，让天下女人，没有不美丽肌肤...',
    bg_color: '235,162,189',
    font_color: '255,255,255',
    index:0,  //tab切换
    imgUrl:'https://gcdn.playonwechat.com/pai/logo.png',
    showDialog: true //广告图
  },
  //事件处理函数
  onLoad: function () {
    let that = this;
    setTimeout(function () {
      that.setData({
        showDialog: false
      })
    }, 3000)
    //ad
    wx.request({
      url: "https://unify.playonweixin.com/site/get-advertisements",
      success: function (res) {
        console.log(res);
        if (res.data.status) {
          var advers = res.data.adver.advers;
          var head_adver = res.data.adver.head_adver;
          var broadcasting = res.data.adver.broadcasting;
          wx.setStorageSync("advers", advers);
          wx.setStorageSync("broadcasting", broadcasting);
          that.setData({
            broadcasting,
            head_adver
          })
        }
      }
    })
  },
  onShow:function(){
    let that = this;
    app.getAuth(function () {
      let userInfo = wx.getStorageSync('userInfo');
      let sign = wx.getStorageSync('sign');
    })
  },
  // 文字
  textSpeak(e) {
    let that = this;
    let text = e.detail.value;
    console.log(text);
    that.setData({
      text: text
    });
    setTimeout(function () {
      that.setData({
        imgUrl: apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid + '&text=' + that.data.text + '&bg_color=' + that.data.bg_color + '&font_color=' + that.data.font_color
      })
    }, 20)
      // wx.request({
    //   url: apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid,
    //   data: {
    //     text: that.data.text,
    //     bg_color: that.data.bg_color,
    //     font_color: that.data.font_color
    //   },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   method: "GET",
    //   success: function (res) {
    //     console.log(apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid + '&text=' + that.data.text + '&bg_color=' + that.data.bg_color + '&font_color=' + that.data.font_color)
    //     setTimeout(function(){
    //         that.setData({
    //           imgUrl: apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid + '&text=' + that.data.text + '&bg_color=' + that.data.bg_color + '&font_color=' + that.data.font_color
    //         })
    //     },20)

    //     wx.hideLoading()
    //   }
    // })
  },
  //bgColor
  bgColor(e){
    let that = this;
    let sign = wx.getStorageSync('sign');
    let bg_color = e.currentTarget.dataset.bgcolor;
    //console.log('bg_color:', bg_color);
    that.setData({
      bg_color: bg_color
    });
    setTimeout(function () {
      that.setData({
        imgUrl: apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid + '&text=' + that.data.text + '&bg_color=' + that.data.bg_color + '&font_color=' + that.data.font_color
      })
    }, 20)
  },
  fontColor(e){
    let that = this;
    let sign = wx.getStorageSync('sign');
    let font_color = e.currentTarget.dataset.fontcolor;
    //console.log('font_color:', font_color);
    that.setData({
      font_color: font_color
    });
    setTimeout(function () {
        that.setData({
          imgUrl: apiurl + "placards/images?sign=" + sign + '&operator_id=' + app.data.kid + '&text=' + that.data.text + '&bg_color=' + that.data.bg_color + '&font_color=' + that.data.font_color
        })
    },20)
  },
 
  
  taps(e){
    let that = this;
    let sign = wx.getStorageSync('sign');
    let index = e.currentTarget.dataset.index;
    that.setData({
      index
    })
    if (index==3){
      wx.showLoading({
        title: '图片保存中',
      });
      // 下载海报
      console.log("downLoad:", that.data.imgUrl);
        wx.downloadFile({
          url: that.data.imgUrl, //仅为示例，并非真实的资源
          success: function (res) {
            console.log("成功：", res);
            wx.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success(res) {
                console.log(res);
                wx.showToast({
                  title: '图片保存成功，请去相册查看',
                  icon: 'success',
                  duration: 800
                })
              }
            })
          }, 
          fail: function (res) {
            console.log('fail:',res);
            tips.alert('图片保存失败！');
          }
        })
        wx.hideLoading()
    }
    
  },
  // 预览海报
  prewImg: function () {
    console.log("prewImg:", this.data.imgUrl);
    wx.previewImage({
      current: this.data.imgUrl, // 当前显示图片的http链接
      urls: [this.data.imgUrl] // 需要预览的图片http链接列表
    })
  }
 
 
})
